package com.pkdl.forms;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;

import com.pkdl.dao.BenhNhanDAO;
import com.pkdl.datepicker.JCalendar;
import com.pkdl.dto.BenhNhanDTO;
import com.pkdl.tables.TableBenhNhan;

@SuppressWarnings("serial")
public class DlgQuanLyBenhNhan extends JDialog {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			DlgQuanLyBenhNhan dialog = new DlgQuanLyBenhNhan(null, true);
			dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private BenhNhanDAO bnDAO;
	private JTable tblData;
	private JTextField txtMaBN;
	private JTextField txtHoTen;
	private JTextField txtNgheNghiep;
	private JTextField txtDiaChi;
	private JTextField txtDienThoai;
	private JRadioButton rdbtnNam;
	private JRadioButton rdbtnNu;
	private JPanel pnlCalendar;
	private JCalendar txtNgaySinh = null;

	/**
	 * Create the dialog.
	 */
	public DlgQuanLyBenhNhan(Frame parent, boolean modal) {
		super(parent, modal);
		this.setTitle("Quan ly benh nhan");
		this.setBounds(100, 100, 952, 565);
		this.setLocationRelativeTo(null);
		this.getContentPane().setLayout(new BorderLayout());
		{
			JPanel panel = new JPanel();
			this.getContentPane().add(panel, BorderLayout.CENTER);
			panel.setLayout(new BorderLayout(0, 0));
			{
				JPanel panel_1 = new JPanel();
				panel_1.setPreferredSize(new Dimension(452, 10));
				panel.add(panel_1, BorderLayout.WEST);
				panel_1.setLayout(new BorderLayout(0, 0));
				{
					JPanel panel_2 = new JPanel();
					panel_2.setBackground(SystemColor.textHighlight);
					panel_1.add(panel_2, BorderLayout.NORTH);
					panel_2.setLayout(new BorderLayout(0, 0));
					{
						JLabel lblNewLabel = new JLabel(
								"  Danh s\u00E1ch b\u1EC7nh nh\u00E2n");
						lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
						lblNewLabel.setForeground(Color.WHITE);
						lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
						lblNewLabel.setPreferredSize(new Dimension(46, 32));
						panel_2.add(lblNewLabel, BorderLayout.CENTER);
					}
				}
				{
					JScrollPane scrollPane = new JScrollPane();
					panel_1.add(scrollPane, BorderLayout.CENTER);
					{
						this.tblData = new JTable();
						this.tblData.addMouseListener(new MouseAdapter() {
							@Override
							public void mousePressed(MouseEvent evt) {
								DlgQuanLyBenhNhan.this.actionSelect(evt);
							}
						});
						this.tblData.setFillsViewportHeight(true);
						this.tblData.setModel(new DefaultTableModel(
								new Object[][] { { null, null, null },
										{ null, null, null },
										{ null, null, null }, }, new String[] {
										"New column", "New column",
										"New column" }));
						scrollPane.setViewportView(this.tblData);
					}
				}
			}
			{
				JPanel panel_1 = new JPanel();
				panel.add(panel_1, BorderLayout.CENTER);
				panel_1.setLayout(null);

				JLabel lblNewLabel_1 = new JLabel(
						"Th\u00F4ng tin b\u1EC7nh nh\u00E2n");
				lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 18));
				lblNewLabel_1.setForeground(new Color(0, 128, 0));
				lblNewLabel_1.setBounds(10, 0, 287, 31);
				panel_1.add(lblNewLabel_1);

				JLabel lblNewLabel_2 = new JLabel("M\u00E3 BN");
				lblNewLabel_2.setBounds(27, 45, 46, 14);
				panel_1.add(lblNewLabel_2);

				this.txtMaBN = new JTextField();
				this.txtMaBN.setEditable(false);
				this.txtMaBN.setBounds(111, 42, 86, 20);
				panel_1.add(this.txtMaBN);
				this.txtMaBN.setColumns(10);
				{
					JLabel lblTnDv = new JLabel("H\u1ECD t\u00EAn");
					lblTnDv.setBounds(27, 73, 46, 14);
					panel_1.add(lblTnDv);
				}
				{
					this.txtHoTen = new JTextField();
					this.txtHoTen.setColumns(10);
					this.txtHoTen.setBounds(111, 70, 219, 20);
					panel_1.add(this.txtHoTen);
				}
				{
					JLabel lblPhDv = new JLabel("Ng\u00E0y sinh");
					lblPhDv.setBounds(27, 113, 74, 14);
					panel_1.add(lblPhDv);
				}
				{
					JLabel lblMT = new JLabel("Gi\u1EDBi t\u00EDnh");
					lblMT.setBounds(27, 315, 65, 14);
					panel_1.add(lblMT);
				}

				JButton btnLuu = new JButton("L\u01B0u");
				btnLuu.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						DlgQuanLyBenhNhan.this.actionLuu();
					}
				});
				btnLuu.setIcon(new ImageIcon(DlgQuanLyBenhNhan.class
						.getResource("/com/pkdl/resources/45.png")));
				btnLuu.setBounds(386, 451, 89, 23);
				panel_1.add(btnLuu);

				JButton btnXoa = new JButton("X\u00F3a");
				btnXoa.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						DlgQuanLyBenhNhan.this.actionDelete();
					}
				});
				btnXoa.setIcon(new ImageIcon(DlgQuanLyBenhNhan.class
						.getResource("/com/pkdl/resources/001_29.png")));
				btnXoa.setBounds(287, 451, 89, 23);
				panel_1.add(btnXoa);

				JButton btnThemMoi = new JButton("Th\u00EAm m\u1EDBi");
				btnThemMoi.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						DlgQuanLyBenhNhan.this.resetFields();
					}
				});
				btnThemMoi.setIcon(new ImageIcon(DlgQuanLyBenhNhan.class
						.getResource("/com/pkdl/resources/Eraser-icon.png")));
				btnThemMoi.setBounds(165, 451, 112, 23);
				panel_1.add(btnThemMoi);

				this.rdbtnNam = new JRadioButton("Nam");
				this.rdbtnNam.setSelected(true);
				this.rdbtnNam.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						DlgQuanLyBenhNhan.this.rdbtnNu.setSelected(false);
					}
				});
				this.rdbtnNam.setBounds(111, 311, 65, 23);
				panel_1.add(this.rdbtnNam);

				this.rdbtnNu = new JRadioButton("N\u1EEF");
				this.rdbtnNu.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						DlgQuanLyBenhNhan.this.rdbtnNam.setSelected(false);
					}
				});
				this.rdbtnNu.setBounds(189, 311, 65, 23);
				panel_1.add(this.rdbtnNu);

				JLabel lblNghNghip = new JLabel("Ngh\u1EC1 nghi\u1EC7p");
				lblNghNghip.setBounds(27, 340, 74, 14);
				panel_1.add(lblNghNghip);

				this.txtNgheNghiep = new JTextField();
				this.txtNgheNghiep.setColumns(10);
				this.txtNgheNghiep.setBounds(111, 337, 219, 20);
				panel_1.add(this.txtNgheNghiep);

				this.txtDiaChi = new JTextField();
				this.txtDiaChi.setColumns(10);
				this.txtDiaChi.setBounds(111, 368, 364, 20);
				panel_1.add(this.txtDiaChi);

				this.txtDienThoai = new JTextField();
				this.txtDienThoai.setColumns(10);
				this.txtDienThoai.setBounds(111, 399, 157, 20);
				panel_1.add(this.txtDienThoai);

				JLabel lblaCh = new JLabel("\u0110\u1ECBa ch\u1EC9");
				lblaCh.setBounds(27, 371, 74, 14);
				panel_1.add(lblaCh);

				JLabel lblinThoi = new JLabel("\u0110i\u1EC7n tho\u1EA1i");
				lblinThoi.setBounds(27, 402, 74, 14);
				panel_1.add(lblinThoi);

				this.pnlCalendar = new JPanel();
				this.pnlCalendar.setBounds(111, 101, 219, 203);
				panel_1.add(this.pnlCalendar);
				this.pnlCalendar.setLayout(new BorderLayout(0, 0));
			}
		}
		this.LoadCalendar();
		this.loadData();
	}

	/**
	 * Delete item
	 */
	private void actionDelete() {
		if (this.txtMaBN.getText().trim().equals("")) {
			JOptionPane.showMessageDialog(null, "Vui long chon dong de xoa",
					"Announcement", JOptionPane.ERROR_MESSAGE);
			return;
		}
		this.bnDAO = new BenhNhanDAO();
		if (this.bnDAO.delete(Integer.parseInt(this.txtMaBN.getText()))) {
			this.loadData();
			this.resetFields();
			JOptionPane.showMessageDialog(null, "Xoa du lieu thanh cong",
					"Announcement", JOptionPane.INFORMATION_MESSAGE);
		} else {
			JOptionPane.showMessageDialog(null, "Khong the xoa du lieu",
					"Announcement", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * Them moi, xoa, cap nhat danh sach thuoc
	 */
	private void actionLuu() {
		AtomicReference<Object> errText = new AtomicReference<Object>("");
		if (!this.validateInputData(errText)) {
			JOptionPane.showMessageDialog(null, errText.get(), "Announcement",
					JOptionPane.ERROR_MESSAGE);
			return;
		}

		this.bnDAO = new BenhNhanDAO();

		// Them moi
		if (this.txtMaBN.getText().trim().equals("")) {
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.MONTH, this.txtNgaySinh.getMonthChooser()
					.getMonth());
			cal.set(Calendar.DATE, this.txtNgaySinh.getDayChooser().getDay());
			cal.set(Calendar.YEAR, this.txtNgaySinh.getYearChooser().getYear());

			BenhNhanDTO item = new BenhNhanDTO(this.txtHoTen.getText(),
					cal.getTime(), this.rdbtnNam.isSelected() ? true : false,
					this.txtNgheNghiep.getText(), this.txtDiaChi.getText(),
					this.txtDienThoai.getText());
			if (this.bnDAO.insert(item)) {
				this.loadData();
				this.resetFields();
				JOptionPane.showMessageDialog(null, "Them du lieu thanh cong",
						"Announcement", JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null, "Khong the them du lieu",
						"Announcement", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			// Cap nhat
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.MONTH, this.txtNgaySinh.getMonthChooser()
					.getMonth());
			cal.set(Calendar.DATE, this.txtNgaySinh.getDayChooser().getDay());
			cal.set(Calendar.YEAR, this.txtNgaySinh.getYearChooser().getYear());

			BenhNhanDTO item = this.bnDAO.selectById(Integer
					.parseInt(this.txtMaBN.getText()));
			item.setHoTen(this.txtHoTen.getText());
			item.setNgaySinh(cal.getTime());
			item.setGioiTinh(this.rdbtnNam.isSelected() ? true : false);
			item.setNgheNghiep(this.txtNgheNghiep.getText());
			item.setDiaChi(this.txtDiaChi.getText());
			item.setDienThoai(this.txtDienThoai.getText());
			if (this.bnDAO.update(item)) {
				this.loadData();
				JOptionPane.showMessageDialog(null,
						"Cap nhat du lieu thanh cong", "Announcement",
						JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(null,
						"Khong the cap nhat du lieu", "Announcement",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	/**
	 * Lua chon item trong danh sach
	 */
	private void actionSelect(MouseEvent evt) {
		int modifiers = evt.getModifiers();
		if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
			if (this.tblData.getSelectedRow() == -1) {
				return;
			}

			if (this.tblData.getValueAt(this.tblData.getSelectedRow(), 0) == null) {
				return;
			}

			if (evt.getClickCount() == 1) {
				this.bnDAO = new BenhNhanDAO();
				String id = ((JLabel) this.tblData.getValueAt(
						this.tblData.getSelectedRow(), 0)).getName().trim();
				BenhNhanDTO item = this.bnDAO.selectById(Integer.valueOf(id));
				this.txtMaBN.setText(String.valueOf(item.getMaBenhNhan()));
				this.txtHoTen.setText(item.getHoTen());
				this.txtDiaChi.setText(item.getDiaChi());
				this.txtDienThoai.setText(item.getDienThoai());
				this.txtNgheNghiep.setText(item.getNgheNghiep());
				if (item.getGioiTinh()) {
					this.rdbtnNam.setSelected(true);
					this.rdbtnNu.setSelected(false);
				} else {
					this.rdbtnNam.setSelected(false);
					this.rdbtnNu.setSelected(true);
				}
				this.txtNgaySinh.setDate(item.getNgaySinh());
			}
		}
	}

	/**
	 * Tao calendar ngay sinh
	 */
	public void LoadCalendar() {
		if (this.txtNgaySinh == null) {
			this.txtNgaySinh = new JCalendar();
			this.pnlCalendar.add(this.txtNgaySinh);
		}
	}

	/**
	 * Load danh sach thuoc
	 */
	private void loadData() {
		this.bnDAO = new BenhNhanDAO();
		List<BenhNhanDTO> ds = this.bnDAO.selectAll();
		TableBenhNhan table = new TableBenhNhan(ds);
		table.loadDataIntoTable(this.tblData);
	}

	/**
	 * Reset fields
	 */
	private void resetFields() {
		this.txtMaBN.setText("");
		this.txtHoTen.setText("");
		// this.txtNgaySinh.setText("");
		this.txtDiaChi.setText("");
		this.txtDienThoai.setText("");
		this.txtNgheNghiep.setText("");
		this.txtNgaySinh.setDate(new Date());
	}

	/**
	 * Validate user input
	 * 
	 * @return true: valid / false: invalid
	 */
	private boolean validateInputData(AtomicReference<Object> errText) {
		if (this.txtHoTen.getText().trim().equals("")) {
			errText.set("Ban chua nhap ho ten");
			return false;
		}
		if (this.txtDienThoai.getText().trim().equals("")) {
			errText.set("Ban chua nhap dien thoai");
			return false;
		}
		return true;
	}
}
